<?php


class tx_zlib_snippet
{

    /**
     * @var array
     */
    private $paths = array();

    /**
     * @var array
     */
    private $vars = array();

    /**
     * @param string $path
     */
    public function addPath($path)
    {
        array_unshift($this->paths, rtrim($path, '/'));
    }

    /**
     * @param string|array $spec
     * @param mixed $value
     */
    public function assign($spec, $value = null)
    {
        if (is_string($spec)) {
	        $this->vars[$spec] = $value;
        } elseif (is_array($spec)) {
            foreach ($spec as $key => $val) {
                $this->vars[$key] = $val;
            }
        } else {
            throw new Exception('assign() expects a string or array, received ' . gettype($spec), $this);
        }
    }

    /**
     * @return void
     */
    public function clearVars()
    {
        $this->vars = array();
    }

    /**
     * @return array
     */
    public function getPaths()
    {
        return $this->paths;
    }

    /**
     * @param string $snippet
     * @return string
     */
    public function render($snippet)
    {
        $snippetPath = $this->getSnippetPath($snippet);
        $content = file_get_contents($snippetPath);
        return $this->replacePlaceholders($content);
    }

    /**
     * @param string $content
     * @return string
     */
    private function replacePlaceholders($content)
    {
        $replacements = array();
        foreach($this->vars AS $key=>$value) {
            $replacements['{{' . $key . '}}'] = $value;
        }
        return str_replace(
            array_keys($replacements),
            array_values($replacements),
            $content
        );
    }

    /**
     * @param string $key
     * @return mixed
     */
    protected function __get($key)
    {
        if (isset($this->vars[$key])) {
            return $this->vars[$key];
        }
        return null;
    }

    /**
     * @param string $key
     * @return boolean
     */
    protected function __isset($key)
    {
        return isset($this->vars[$key]);
    }

    /**
     * @param string $key
     * @param mixed $value
     */
    protected function __set($key, $value)
    {
        $this->vars[$key] = $value;
    }

    /**
     * @param string $key
     */
    protected function __unset($key)
    {
        unset($this->vars[$key]);
    }

    /**
     * @param string $snippet
     * @return string
     */
    private function getSnippetPath($snippet)
    {
        foreach($this->paths AS $path) {
            $snippetPath = $path . '/' . $snippet . '.spt';
            if(is_file($snippetPath)) {
                return $snippetPath;
            }
        }
        throw new Exception("Snippet $snippet.spt not found in " . implode(",", $this->paths));
    }

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_snippet.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_snippet.php']);
}
